package com.boyu.backend;

import com.boyu.common.BaseController;
import com.boyu.common.MenuLogic;
import com.boyu.common.SystemConst;
import com.boyu.common.interceptor.AdminAuthInterceptor;
import com.boyu.common.model.User;
import com.boyu.common.showmodel.ShowMenu;
import com.boyu.common.utils.StringUtils;
import com.jfinal.aop.Clear;

import java.util.List;

/**
 * IndexController
 */
public class _IndexController extends BaseController {
    public static final String LOGIN_HTML = "login.html";

    public void index() {
        List<ShowMenu> menus = MenuLogic.getInstance().menuLoad();
        setSessionAttr(SystemConst.ADMIN_MENU, menus);
        render(INDEX_HTML);
    }

    @Clear(AdminAuthInterceptor.class)
    public void login() {
        String username = getPara("username");
        String password = getPara("password");
        String pwdMd5 = StringUtils.encoderByMd5(password);
        User first = User.dao.findFirst("select * from t_user where name =? and password=? ", username, pwdMd5);
        if (first == null) {
            setAttr("message","用户名或密码错误");
            toLogin();
            return;
        }
        setSessionAttr(SystemConst.ADMIN_USER_SESSION,first);
        index();
    }
    @Clear(AdminAuthInterceptor.class)
    public void toLogin() {
        render(LOGIN_HTML);
    }

    public void logout() {
        getSession().removeAttribute(SystemConst.ADMIN_USER_SESSION);
        render(LOGIN_HTML);
    }
}





